/*var ws = new WebSocket('ws://192.168.1.109:15674/ws');
// 获得Stomp client对象
var client = Stomp.over(ws);
// SockJS does not support heart-beat: disable heart-beats
client.heartbeat.outgoing = 0;
client.heartbeat.incoming = 0;

// 定义连接成功回调函数
var on_connect = function(x) {
    //data.body是接收到的数据
    client.subscribe("/queue/fanoutExchange", function(data) {
        var msg = data.body;
        console.log(msg);
    });
};

// 定义错误时回调函数
var on_error =  function() {
    console.log('error');
    client.connect('root', '123456', on_connect, on_error, '/');
};

// 连接RabbitMQ
client.connect('root', '123456', on_connect, on_error, '/');
console.log(">>>连接上http://192.168.1.109:15674/ws");*/
var mqtt = {
    hostname : "192.168.1.109",
    port : 8083,
    clientId : "client_web",
    timeout : 5,
    keepAlive : 50,
    cleanSession : true,
    ssl : false,
    userName : layui.data("sysUser").oid,
    password : "123456",
    topic : "hello",
    client : null,
};
mqtt.client = new Paho.MQTT.Client(mqtt.hostname, mqtt.port, mqtt.clientId);
var mqtt_client_options = {
    invocationContext: {
        host : mqtt.hostname,
        port: mqtt.port,
        path: mqtt.client.path,
        clientId: mqtt.clientId
    },
    timeout: mqtt.timeout,
    keepAliveInterval: mqtt.keepAlive,
    cleanSession: mqtt.cleanSession,
    useSSL: mqtt.ssl,
    userName: mqtt.userName,
    password: mqtt.password,
    onSuccess: mqtt_onConnect,
    onFailure: function(e){
        console.log(e);
    }
};
//连接服务器并注册连接成功处理事件
function mqtt_onConnect() {
    console.log("链接mqtt成功");
    mqtt.client.subscribe(mqtt.topic);
}
//注册消息接收处理事件
function mqtt_onConnectionLost(responseObject) {
    console.log(responseObject);
    if (responseObject.errorCode !== 0) {
        console.log("onConnectionLost:"+responseObject.errorMessage);
        console.log("连接已断开");
        setTimeout(function () {
            $("#LAY_app > div > div.layui-header > ul.layui-nav.layui-layout-right > li:nth-child(2) > dl > dd:nth-child(3) > a > i").click();
        },3000);
    }
}
function mqtt_onMessageArrived(message) {
    console.log("收到消息:"+message.payloadString);
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0, //不显示关闭按钮
        shade: [0],
        area: ['340px', '215px'],
        offset: 'rb', //右下角弹出
        time: 5000, //2秒后自动关闭
        anim: 2,
        content: "<div style='color:red'>"+message.payloadString+"</div>" //弹出的信息写在这个页面
    });
}
function mqtt_start(){
    mqtt.client.connect(mqtt_client_options);
    mqtt.client.onConnectionLost = mqtt_onConnectionLost;
    mqtt.client.onMessageArrived = mqtt_onMessageArrived;
}
//mqtt_start();
/*
var hostname = '192.168.1.109',
    port = 8083,
    clientId = 'client-mao2080',
    timeout = 5,
    keepAlive = 50,
    cleanSession = false,
    ssl = false,
    userName = 'mao2080',
    password = '123',
    topic = 'hello';
client = new Paho.MQTT.Client(hostname, port, clientId);
//建立客户端实例
var options = {
    invocationContext: {
        host : hostname,
        port: port,
        path: client.path,
        clientId: clientId
    },
    timeout: timeout,
    keepAliveInterval: keepAlive,
    cleanSession: cleanSession,
    useSSL: ssl,
    userName: userName,
    password: password,
    onSuccess: onConnect,
    onFailure: function(e){
        console.log(e);
    }
};
client.connect(options);
//连接服务器并注册连接成功处理事件
function onConnect() {
    console.log("onConnected");
    client.subscribe(topic);
}

client.onConnectionLost = onConnectionLost;

//注册连接断开处理事件
client.onMessageArrived = onMessageArrived;

//注册消息接收处理事件
function onConnectionLost(responseObject) {
    console.log(responseObject);
    if (responseObject.errorCode !== 0) {
        console.log("onConnectionLost:"+responseObject.errorMessage);
        console.log("连接已断开");
    }
}

function onMessageArrived(message) {
    console.log("收到消息:"+message.payloadString);
}*/
